package org.hiside.android.travel;

import java.lang.reflect.Field;
import java.util.ArrayList;

import org.hiside.android.travel.line.LineCategoryActivity;
import org.springframework.util.Assert;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    protected static final String TAG = MainActivity.class.getSimpleName();

    private static final String[] COUNTRIES = {"北京故宫博物院","北京颐和园","大同云冈石窟",  
        "大连金石滩景区","苏州园林","无锡鼋头渚景区","苏州金鸡湖景区","嘉兴南湖旅游区 ","黄山市黄山风景区"};  
    
    private Gallery hotSightGallery;

    private Gallery nearSightGallery;

    private Gallery currentSightGallery;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setupGallery();

        // 创建一个ArrayAdapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
                COUNTRIES);
        // 获取AutoCompleteTextView对象
        AutoCompleteTextView autoComplete = (AutoCompleteTextView) findViewById(R.id.sight_name);
        // 将AutoCompleteTextView与ArrayAdapter进行绑定
        autoComplete.setAdapter(adapter);
        // 设置AutoCompleteTextView输入1个字符就进行提示
        autoComplete.setThreshold(1);
    }

    private void setupGallery() {
        hotSightGallery = (Gallery) findViewById(R.id.gallery_sight_hot);
        nearSightGallery = (Gallery) findViewById(R.id.gallery_sight_near);
        currentSightGallery = (Gallery) findViewById(R.id.gallery_sight_current);

        try {
            hotSightGallery.setAdapter(new ImageAdapter(this, "sight_hot"));
            nearSightGallery.setAdapter(new ImageAdapter(this, "sight_near"));
            currentSightGallery.setAdapter(new ImageAdapter(this, "sight_current"));
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        hotSightGallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) {
                startActivity(new Intent(parentView.getContext(), LineCategoryActivity.class));
            }
        });
        nearSightGallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) {
                startActivity(new Intent(parentView.getContext(), LineCategoryActivity.class));
            }
        });
        currentSightGallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) {
                startActivity(new Intent(parentView.getContext(), LineCategoryActivity.class));
            }
        });

        hotSightGallery.setSelection(Integer.MAX_VALUE / 2);
        nearSightGallery.setSelection(Integer.MAX_VALUE / 2);
        currentSightGallery.setSelection(Integer.MAX_VALUE / 2);
    }

    public void gotoLineCategoryView(AdapterView<?> parentView, View childView, int position, long id) {
        // TODO
        MainActivity.this.setTitle(String.valueOf(position));
        Toast.makeText(this, "功能开发中，敬请期待。。。。", Toast.LENGTH_LONG).show();
    }

    public void gotoLineCategoryView(View view) {
        // TODO
        TextView sightName = (TextView) findViewById(R.id.sight_name);
        Toast.makeText(this, "功能开发中，敬请期待。。。。", Toast.LENGTH_LONG).show();
        startActivity(new Intent(MainActivity.this, LineCategoryActivity.class));
    }

    /*
     * class ImageAdapter is used to control gallery source and operation.
     */
    private class ImageAdapter extends BaseAdapter {
        private Context mContext;

        int mGalleryItemBackground;

        private ArrayList<Integer> imgList = new ArrayList<Integer>();

        private ArrayList<Object> imgSizes = new ArrayList<Object>();

        public ImageAdapter(Context c, String filterName) throws IllegalArgumentException, IllegalAccessException {
            Assert.notNull(filterName, "image adapter filter name should not be null.");
            mContext = c;

            TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
            mGalleryItemBackground = typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);

            // 用反射机制来获取资源中的图片ID和尺寸
            Field[] fields = R.drawable.class.getDeclaredFields();
            for (Field field : fields) {
                Log.i(TAG, "field name=" + field.getName());
                if (field.getName().contains(filterName))// 除了icon之外的图片
                {
                    int index = field.getInt(R.drawable.class);
                    // 保存图片ID
                    imgList.add(index);
                    // 保存图片大小
                    int size[] = new int[2];
                    Bitmap bmImg = BitmapFactory.decodeResource(getResources(), index);
                    size[0] = bmImg.getWidth();
                    size[1] = bmImg.getHeight();
                    imgSizes.add(size);
                }
            }
        }

        @Override
        public int getCount() {
            // return imgList.size();
            return Integer.MAX_VALUE;
        }

        @Override
        public Object getItem(int position) {

            return position;
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ImageView i = new ImageView(mContext);
            // 从imgList取得图片ID
            Log.i(TAG, "position=" + position + ", size" + imgList.size());
            position = position % (imgList.size() - 1);
            i.setImageResource(imgList.get(position).intValue());
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            // 从imgSizes取得图片大小
            int size[] = new int[2];
            size = (int[]) imgSizes.get(position);
            i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));
            // 设置Gallery组件的背景风格
            i.setBackgroundResource(mGalleryItemBackground);
            return i;
        }
    }
}
